/**
 * 
 */
package co.gov.medellin.sdep.sisdep.cco.service;

import java.util.Date;
import java.util.List;

import co.gov.medellin.sdep.sisdep.cco.exception.LocalInventarioException;
import co.gov.medellin.sdep.sisdep.cco.exception.LocalInventarioRuntimeException;
import co.gov.medellin.sdep.sisdep.cco.exception.descriptor.LocalInventarioMessageDescriptor;
import co.gov.medellin.sdep.sisdep.common.message.MessageDescriptorConstant;
import co.gov.medellin.sdep.sisdep.common.service.BaseService;
import co.gov.medellin.sdep.sisdep.model.LocalInventario;
import co.gov.medellin.sdep.sisdep.model.dao.impl.ILocalInventarioDAOT;

/**
 * @author Usuario
 *
 */
public class LocalInventarioServiceImpl extends BaseService implements ILocalInventarioService {

	//----------------------------------------------------------------------
	//--- propiedades y constructores                                 init()
	//----------------------------------------------------------------------

	/**
	 * default serial version
	 */
	private static final long serialVersionUID = 1L;
	/** */
	private ILocalInventarioDAOT localInventarioDAO;

	/**
	 * 
	 */
	public LocalInventarioServiceImpl() {
	}

	//----------------------------------------------------------------------
	//--- propiedades y constructores                                  end()
	//----------------------------------------------------------------------

	//----------------------------------------------------------------------
	//--- implementacion de metodos para acciones                     init()
	//----------------------------------------------------------------------

	/**
	 * 
	 */
	public void insert(LocalInventario localInventario) throws LocalInventarioException {
		try{
			localInventario.setUsuarioCrea(getPrincipalUser());
			localInventario.setFechaCrea(new Date());
			getLocalInventarioDAO().insert(localInventario);
		}catch (Exception e) {
			Object [] infoError = {
			};
			if(e.getCause().toString().indexOf("UNIQUE KEY") >= 0){
				throw new LocalInventarioException(new LocalInventarioMessageDescriptor(MessageDescriptorConstant.UK_VIOLATION), infoError, e);
			}else{
				throw new LocalInventarioException(new LocalInventarioMessageDescriptor(MessageDescriptorConstant.SAVE), infoError, e);
			}
		}
	}

	/**
	 * 
	 */
	public final void update(LocalInventario localInventario) throws LocalInventarioException {
		try{
			localInventario.setUsuarioModifica(getPrincipalUser());
			localInventario.setFechaModifica(new Date());
			getLocalInventarioDAO().update(localInventario);
		}catch (Exception e) {
			Object [] infoError = {
			};
			if(e.getCause().toString().indexOf("UNIQUE KEY") >= 0){
				throw new LocalInventarioRuntimeException(new LocalInventarioMessageDescriptor(MessageDescriptorConstant.UK_VIOLATION), infoError, e);
			}else{
				throw new LocalInventarioRuntimeException(new LocalInventarioMessageDescriptor(MessageDescriptorConstant.UPDATE), infoError, e);
			}
		}
	}

	/**
	 * 
	 */
	public final void delete(LocalInventario localInventario) throws LocalInventarioException {
		try {
			localInventario.setUsuarioModifica(getPrincipalUser());
			localInventario.setFechaModifica(new Date());
			getLocalInventarioDAO().delete(localInventario);
		}catch (Exception e) {
			Object [] infoError = {
			};
			throw new LocalInventarioRuntimeException(new LocalInventarioMessageDescriptor(MessageDescriptorConstant.DELETE), infoError, e);
		}
	}

	/**
	 * @return 
	 * 
	 */
	public final List load(int start,int max, LocalInventario localInventario) throws LocalInventarioException {
		try{
			//return getLocalInventarioDAO().getAllLocalInventarioPaginated(start, max, criteria);
			return getLocalInventarioDAO().load(localInventario);
		}catch (Exception e) {
			Object [] infoError = null;
			throw new LocalInventarioRuntimeException(new LocalInventarioMessageDescriptor(MessageDescriptorConstant.RETRIEVE), infoError, e);
		}
	}

	/**
	 * 
	 */
	public final LocalInventario get(long id) throws LocalInventarioException {
		try{
			return getLocalInventarioDAO().findByPrimaryKey(id);
		}catch (Exception e) {
			Object [] infoError = {new Long(id)};
			throw new LocalInventarioRuntimeException(new LocalInventarioMessageDescriptor(MessageDescriptorConstant.GET), infoError, e);
		}
	}

	//----------------------------------------------------------------------
	//--- implementacion de metodos para acciones                      end()
	//----------------------------------------------------------------------

	//----------------------------------------------------------------------
	//--- getters and setters                                         init()
	//----------------------------------------------------------------------

	/**
	 * @author Usuario
	 *
	 */
	public ILocalInventarioDAOT getLocalInventarioDAO() {
		return localInventarioDAO;
	}

	/**
	 * @author Usuario
	 *
	 */
	public void setLocalInventarioDAO(ILocalInventarioDAOT localInventarioDAO) {
		this.localInventarioDAO = localInventarioDAO;
	}

	//----------------------------------------------------------------------
	//--- getters and setters                                          end()
	//----------------------------------------------------------------------

}
